import request from './client';
import { API_ENDPOINTS } from './config';
import {
  UserLoginRequest,
  UserRegisterRequest,
  UserUpdateRequest,
  TokenResponse,
  ArtworkCreateRequest,
  ArtworkUpdateRequest,
  ArtworkResponse,
  MetadataCreateRequest,
  MetadataResponse,
  CommentCreateRequest,
  CommentResponse,
  TopicCreateRequest,
  TopicResponse,
  TopicCommentCreateRequest,
  TopicCommentUpdateRequest,
  TopicCommentResponse,
  FollowCreateRequest,
  FollowResponse,
  PaginatedResponse,
  ApiResponse,
  CommentIn,
  FileResponse,
  FileListResponse,
} from './types';

// 用户服务
export const userService = {
  // 用户注册
  register: (data: UserRegisterRequest) =>
    request<TokenResponse>({
      url: API_ENDPOINTS.REGISTER,
      method: 'POST',
      data,
    }),

  // 用户登录
  login: (data: UserLoginRequest) =>
    request<TokenResponse>({
      url: API_ENDPOINTS.LOGIN,
      method: 'POST',
      data,
    }),

  // 用户登出
  logout: () =>
    request({
      url: API_ENDPOINTS.LOGOUT,
      method: 'POST',
    }),

  // 获取用户信息
  getUser: (userId: number) =>
    request({
      url: API_ENDPOINTS.GET_USER(userId),
      method: 'GET',
    }),

  // 更新用户信息
  updateUser: (userId: number, data: UserUpdateRequest) =>
    request({
      url: API_ENDPOINTS.UPDATE_USER(userId),
      method: 'PATCH',
      data,
    }),
};

// 作品服务
export const artworkService = {
  // 获取标签列表
  getTags: () =>
    request({
      url: API_ENDPOINTS.GET_TAGS,
      method: 'GET',
    }),

  // 获取作品列表
  listArtworks: (params?: { category?: string; tag?: string; keywords?: string; limit?: number; offset?: number }) =>
    request<PaginatedResponse<ArtworkResponse>>({
      url: API_ENDPOINTS.LIST_ARTWORKS,
      method: 'GET',
      params,
    }),

  // 创建作品
  createArtwork: (data: ArtworkCreateRequest) =>
    request<ArtworkResponse>({
      url: API_ENDPOINTS.CREATE_ARTWORK,
      method: 'POST',
      data,
    }),

  // 获取作品详情
  getArtwork: (artworkId: number) =>
    request<ArtworkResponse>({
      url: API_ENDPOINTS.GET_ARTWORK(artworkId),
      method: 'GET',
    }),

  // 更新作品
  updateArtwork: (artworkId: number, data: ArtworkUpdateRequest) =>
    request<ArtworkResponse>({
      url: API_ENDPOINTS.UPDATE_ARTWORK(artworkId),
      method: 'PUT',
      data,
    }),

  // 删除作品
  deleteArtwork: (artworkId: number) =>
    request({
      url: API_ENDPOINTS.DELETE_ARTWORK(artworkId),
      method: 'DELETE',
    }),
};

// 元数据服务
export const metadataService = {
  // 获取元数据列表
  listMetadata: (params?: { artwork_id?: string; limit?: number; offset?: number }) =>
    request<PaginatedResponse<MetadataResponse>>({
      url: API_ENDPOINTS.LIST_METADATA,
      method: 'GET',
      params,
    }),

  // 创建元数据
  createMetadata: (data: MetadataCreateRequest) =>
    request<MetadataResponse>({
      url: API_ENDPOINTS.CREATE_METADATA,
      method: 'POST',
      data,
    }),

  // 获取元数据详情
  getMetadata: (id: number) =>
    request<MetadataResponse>({
      url: API_ENDPOINTS.GET_METADATA(id),
      method: 'GET',
    }),

  // 更新元数据
  updateMetadata: (id: number, data: MetadataCreateRequest) =>
    request<MetadataResponse>({
      url: API_ENDPOINTS.UPDATE_METADATA(id),
      method: 'PUT',
      data,
    }),

  // 删除元数据
  deleteMetadata: (id: number) =>
    request({
      url: API_ENDPOINTS.DELETE_METADATA(id),
      method: 'DELETE',
    }),
};

// 社区服务
export const communityService = {
  // 获取评论列表
  listComments: (params: { work_id: number; page?: number; size?: number }) =>
    request<{
      code: number;
      message: string;
      data: {
        data: CommentResponse[];
        total: number;
        page: number;
        size: number;
      };
    }>({
      url: API_ENDPOINTS.LIST_COMMENTS,
      method: 'GET',
      params,
    }),

  // 创建评论
  createComment: (data: CommentCreateRequest) =>
    request<{
      code: number;
      message: string;
      data: CommentResponse;
    }>({
      url: API_ENDPOINTS.CREATE_COMMENT,
      method: 'POST',
      data,
    }),

  // 更新评论
  updateComment: (commentId: number, data: CommentIn) =>
    request<ApiResponse<CommentResponse>>({
      url: API_ENDPOINTS.UPDATE_COMMENT(commentId),
      method: 'PUT',
      data,
    }),

  // 删除评论
  deleteComment: (commentId: number) =>
    request({
      url: API_ENDPOINTS.DELETE_COMMENT(commentId),
      method: 'DELETE',
    }),

  // 获取话题列表
  listTopics: (params?: { page?: number; size?: number }) =>
    request<{
      code: number;
      message: string;
      data: {
        data: TopicResponse[];
        page: number;
        size: number;
        total: number;
      };
    }>({
      url: API_ENDPOINTS.LIST_TOPICS,
      method: 'GET',
      params,
    }),

  // 创建话题
  createTopic: (data: TopicCreateRequest) =>
    request<TopicResponse>({
      url: API_ENDPOINTS.CREATE_TOPIC,
      method: 'POST',
      data,
    }),

  // 获取话题详情
  getTopic: (topicId: number) =>
    request<TopicResponse>({
      url: API_ENDPOINTS.GET_TOPIC(topicId),
      method: 'GET',
    }),

  // 点赞话题
  likeTopic: (topicId: number) =>
    request<ApiResponse<void>>({
      url: API_ENDPOINTS.LIKE_TOPIC(topicId),
      method: 'POST',
    }),

  // 取消点赞话题
  unlikeTopic: (topicId: number) =>
    request<ApiResponse<void>>({
      url: API_ENDPOINTS.UNLIKE_TOPIC(topicId),
      method: 'DELETE',
    }),

  // 获取话题评论列表
  listTopicComments: (topicId: number, params?: { page?: number; size?: number }) =>
    request<{
      code: number;
      message: string;
      data: {
        data: TopicCommentResponse[];
        total: number;
        page: number;
        size: number;
      };
    }>({
      url: API_ENDPOINTS.LIST_TOPIC_COMMENTS(topicId),
      method: 'GET',
      params,
    }),

  // 创建话题评论
  createTopicComment: (topicId: number, data: TopicCommentCreateRequest) =>
    request<ApiResponse<TopicCommentResponse>>({
      url: API_ENDPOINTS.CREATE_TOPIC_COMMENT(topicId),
      method: 'POST',
      data,
    }),

  // 更新话题评论
  updateTopicComment: (commentId: number, data: TopicCommentUpdateRequest) =>
    request<ApiResponse<TopicCommentResponse>>({
      url: API_ENDPOINTS.UPDATE_TOPIC_COMMENT(commentId),
      method: 'PUT',
      data,
    }),

  // 删除话题评论
  deleteTopicComment: (commentId: number) =>
    request<ApiResponse<void>>({
      url: API_ENDPOINTS.DELETE_TOPIC_COMMENT(commentId),
      method: 'DELETE',
    }),

  // 创建关注
  createFollow: (data: FollowCreateRequest) =>
    request<FollowResponse>({
      url: API_ENDPOINTS.CREATE_FOLLOW,
      method: 'POST',
      data,
    }),

  // 取消关注
  deleteFollow: (followId: number) =>
    request({
      url: API_ENDPOINTS.DELETE_FOLLOW(followId),
      method: 'DELETE',
    }),
};

// 文件服务
export const fileService = {
  // 上传文件
  uploadFile: (file: FormData) =>
    request<FileResponse>({
      url: API_ENDPOINTS.UPLOAD_FILE,
      method: 'POST',
      headers: {
        'Content-Type': 'multipart/form-data',
      },
      data: file,
    }),

  // 获取文件列表
  listFiles: () =>
    request<FileListResponse>({
      url: API_ENDPOINTS.LIST_FILES,
      method: 'GET',
    }),

  // 删除文件
  deleteFile: (fileId: number) =>
    request<ApiResponse<void>>({
      url: API_ENDPOINTS.DELETE_FILE(fileId),
      method: 'DELETE',
    }),
}; 